package com.wsc.wisdomchinaqinghe.activitity.fourmodule;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.wsc.wisdomchinaqinghe.R;
import com.wsc.wisdomchinaqinghe.Tools.JsonTools;
import com.wsc.wisdomchinaqinghe.Tools.Tools;
import com.wsc.wisdomchinaqinghe.activitity.Login_RegisterActivity;
import com.wsc.wisdomchinaqinghe.activitity.baseactivity.BaseActivity;
import com.wsc.wisdomchinaqinghe.application.MyApplication;
import com.wsc.wisdomchinaqinghe.callback.ContributorView;
import com.wsc.wisdomchinaqinghe.constant.Constant;
import com.wsc.wisdomchinaqinghe.javaBean.ApplyDetailBean;
import com.wsc.wisdomchinaqinghe.javaBean.RespObj;
import com.wsc.wisdomchinaqinghe.presenter.ContributorPresenter;
import com.wsc.wisdomchinaqinghe.view.iphoneDialogBuilder;

/**
 * 助老助残活动
 * Created by ${wangtao} on 2017/3/3.
 */

public class ApplyDetailActivity extends BaseActivity implements View.OnClickListener, ContributorView {
    private Context context;
    private ContributorPresenter contributorPresenter;

    private Button shenqingbanli;
    private ImageView shoulizhongxin_pagewebview_back;
    private TextView page_title;
    private WebView shoulizhongxin_page_web_view;
    private String id;
    private final String PAGEDETAILFLAG = "PAGEDETAILFLAG";
    private ApplyDetailBean newsDetailBean;
    private MyApplication myApplication;
    private int type;
    private String cid;
    private TextView tv_score;
    private String source;
    private String previousId;
    private RelativeLayout pingfen_relativelayout;
    private View view;
    private ImageView iv_shareicon;
    private String titlename;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.applydetail_layout);
        context = this;
        initView();
        myApplication = (MyApplication) getApplication();
        contributorPresenter = new ContributorPresenter(this, context);
        Intent intent = getIntent();
        titlename= intent.getStringExtra("name");
        id = intent.getStringExtra("id");
        if (titlename.length() > 8) {
            page_title.setText("详情");
        } else {
            page_title.setText(titlename);
        }
        cid=intent.getStringExtra("cid");
        type=intent.getIntExtra("type",0);
        source=intent.getStringExtra("source");
        previousId=intent.getStringExtra("previousId");

        contributorPresenter.getBusinessApplyDetail(id, cid, PAGEDETAILFLAG);
    }

    private void initView() {
        shoulizhongxin_pagewebview_back = (ImageView) findViewById(R.id.shoulizhongxin_pagewebview_back);
        page_title = (TextView) findViewById(R.id.page_title);
        shenqingbanli = (Button) findViewById(R.id.shenqingbanli);
        shoulizhongxin_page_web_view = (WebView) findViewById(R.id.shoulizhongxin_page_web_view);
        shoulizhongxin_pagewebview_back.setOnClickListener(this);
        shenqingbanli.setOnClickListener(this);
        tv_score=(TextView) findViewById(R.id.tv_score);
        pingfen_relativelayout=(RelativeLayout) findViewById(R.id.pingfen_relativelayout);
        view=findViewById(R.id.view);
        iv_shareicon=(ImageView) findViewById(R.id.iv_shareicon);
        iv_shareicon.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.shoulizhongxin_pagewebview_back:
                finish();
                break;
            case R.id.shenqingbanli:
                boolean isLogin = Tools.getSharedPreferencesboolen(Constant.isLoginIn, false, context);
                if (isLogin) {
                    int subType=newsDetailBean.getSubType();
                    if(type==1 && subType==0)
                    {
                        //进入法律援助申请表
                        Intent intent=new Intent(context,ApplyPublishActivity.class);
                        intent.putExtra("type",type);
                        intent.putExtra("subType",subType);
                        intent.putExtra("cid",cid);
                        intent.putExtra("name","法律援助登记表");
                        intent.putExtra("source",source);
                        intent.putExtra("previousId",previousId);
                        if(source!=null)
                        {
                            startActivityForResult(intent,0);
                        }else
                        {
                            startActivity(intent);
                        }
                    }else if(type==10 && subType==0){
                        //进入养老金申请表
                        //进入法律援助申请表
                        Intent intent=new Intent(context,PensionPublishActivity.class);
                        intent.putExtra("type",type);
                        intent.putExtra("subType",subType);
                        intent.putExtra("cid",cid);
                        intent.putExtra("name","尊老金发放审批表(1/2)");
                        intent.putExtra("source",source);
                        intent.putExtra("previousId",previousId);
                        if(source!=null)
                        {
                            startActivityForResult(intent,0);
                        }else
                        {
                            startActivity(intent);
                        }
                    }



                } else {
                    iphoneDialogBuilder idb = new iphoneDialogBuilder(context);
                    idb.setTitle("提示");
                    idb.setMessage("是否登录");
                    idb.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            Intent intent = new Intent(context, Login_RegisterActivity.class);
                            myApplication.setSourceIntoLoginRegisterActivity(ApplyDetailActivity.class.getSimpleName());
                            startActivity(intent);

                        }
                    });
                    idb.setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {

                        public void onClick(DialogInterface dialog, int which) {

                        }
                    });
                    idb.show();
                }
                break;
            case R.id.iv_shareicon:
                String url=Constant.SHOULICENTER_DETAIL+"?id="+id+"&cid="+cid;
                String content="我正在关注："+titlename+"的材料";
                Tools.showShare(titlename,url,content,context);
                break;

        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if(requestCode==0 && resultCode==1)
        {
            Intent intent=getIntent();
            setResult(1,intent);
            finish();
        }
    }

    @Override
    public void onLoadContributorStart(String flag) {

    }

    @Override
    public void onLoadContributorSuccess(String jsonString, String flag) {
        try {
            if (flag.equals(PAGEDETAILFLAG)) {
                RespObj respObj = JsonTools.jsonStringToObject(jsonString, RespObj.class);
                String code = respObj.getCode();
                Object object = respObj.getMessage();
                if (code.equals("200") && object != null) {
                    String jsonMessage = JsonTools.objectToJsonString(object);
                    newsDetailBean = JsonTools.jsonStringToObject(jsonMessage, ApplyDetailBean.class);
                    if (newsDetailBean != null) {
                        if(newsDetailBean.getApply()==1)
                        {
                            shenqingbanli.setVisibility(View.VISIBLE);
                            view.setVisibility(View.VISIBLE);
                            pingfen_relativelayout.setVisibility(View.VISIBLE);
                        }else
                        {
                            shenqingbanli.setVisibility(View.GONE);
                            view.setVisibility(View.GONE);
                            pingfen_relativelayout.setVisibility(View.GONE);
                        }
                        tv_score.setText(Tools.div(newsDetailBean.getScore(),1,1)+"");
                        String webView = Constant.Head_string+newsDetailBean.getContent()+Constant.foot_string;//URL编码数据

                        WebSettings settings=shoulizhongxin_page_web_view.getSettings();
                        settings.setJavaScriptEnabled(true);
                        settings.setRenderPriority(WebSettings.RenderPriority.HIGH);
                        settings.setDefaultTextEncodingName("UTF-8");
                        settings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
                        settings.setLoadWithOverviewMode(true);
                        shoulizhongxin_page_web_view.loadData(webView, "text/html; charset=UTF-8", null);
                    }

                }
            }
        } catch (Exception e) {

        }
    }

    @Override
    public void onLoadContributorComplete(String flag) {
    }

    @Override
    public void onLoadContributorError(String error, String flag) {
    }

}
